package com.zzz.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

/*
 * @author zzr
 * @date: 2025/07/06  16:47
 * @description: 测试 HttpServletRequest
 */
@WebServlet("/servlet8")
public class servlet8 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 行相关 GET/POST url http/1.1
        System.out.println(req.getMethod()); // 获取请求方式
        System.out.println(req.getScheme()); // 获取请求协议
        System.out.println(req.getProtocol()); // 获取请求协议及其版本号
        System.out.println(req.getRequestURI()); // 获取请求的 uri ==》 项目内的资源路径
        System.out.println(req.getRequestURL()); // 获取请求 url ==》 项目内资源的完整的路径
        System.out.println(req.getLocalPort()); // 本应用容器的端口号
        System.out.println(req.getServerPort()); // 客户端发请求用的端口号
        System.out.println(req.getRemotePort()); // 客户端软件的端口号

        // 头相关 key：value   key：value
        // 可以根据名字，来单独获取某个请求头
        String accept = req.getHeader("Accept");
        System.out.println("Accept" + accept);

        // 也可以先获取本次请求中的所有请求头的名字
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String hname = headerNames.nextElement();
            System.out.println(hname + ":" + req.getHeader(hname));
        }
    }
}
